package com.mibotiquin.alarmas;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Set;

import com.mibotiquin.DbHelper.BaseUtil;
import com.mibotiquin.DbHelper.DatabaseItem;
import com.mibotiquin.menu.Principal;
import com.mibotiquin.misfarmacos.ItemFarmaco;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Application;

public class AlarmContainer extends Activity {
	
	//This class have all the alarms to manipulate them easily
	private HashMap<Integer, Alarm> alarmList = null;
	
	private static AlarmContainer cont = null;
	
	/** Add the alarm "al" to the container
	 * 
	 * @param al = The alarm to be added
	 * @param toDB = Add this alarm to the database or not
	 * @return int which represents the alarm id
	 */
	public int addAlarm(Alarm al, boolean toDB) {
		
		int id = 0;
		
		if (alarmList == null) {
			
			alarmList = new HashMap<Integer, Alarm>();
			al.setId(id);
			alarmList.put(id, al);
			
		} else {
			
			/* To calculate the new alarm id, and in order to maintain a right amount of values,
			 * first we'll suppose to add 0 id. We'll iterate in the hashmap and when we take an alarm
			 * with its id greater than "id", we use this "id"
			 */
			
			Set<Integer> queue = alarmList.keySet();
			Iterator<Integer> it = queue.iterator();
			boolean b = false;
			
			while (it.hasNext() && !b) {
				
				int i = it.next();
				
				if (i > id)
					b = true;
				else 
					id++;
			}
			
			al.setId(id);
			alarmList.put(id, al);
			
		}
		
		if (toDB) {
			String[] newData = { String.valueOf(id), al.getNombre(), al.getDate(), al.getHourFormated(), al.getFreqSt(), al.getDurSt(),
								al.getDesc(), al.getNombreMed(), al.getLastDate()};
			ItemAlarm newItem = new ItemAlarm(newData);
			final BaseUtil bd = BaseUtil.getInstance(this);
			bd.insertar(newItem);
			
		}
		
		return id;
		
	}
	
	public Alarm getAlarm(int i) {
		
		if (alarmList == null)
			return null;
		else
			return alarmList.get(i);
		
	}
	
	public void cancelAlarm(int i) {
		
		if (alarmList != null) {
			Alarm al = alarmList.get(i);
			if (al != null) {
				//First, we remove the alarm from the database
				String query = "idAlarma="+al.getIdStr();
				final BaseUtil bd = BaseUtil.getInstance(this);
				ItemAlarm item = new ItemAlarm();
				bd.deleteItem(item, query);
				al.cancel();
				alarmList.remove(i);
				//If the alarm exists and we delete it, we have to 
				//relocate the rest of the alarms.
				//To the right allocation, we must delete the alarm event
				//and create it again with the new id
				int lim = alarmList.size()+1;
				for (int j = i+1; j < lim; j++) {
					
					al = alarmList.get(j);
					al.cancel();
					al.setId(j-1);
					alarmList.put(j-1, al);
					
				}
				//If we delete the last alarm, we don't have to delete it again
				if (i != alarmList.size())
					alarmList.remove(alarmList.size()-1);
			}
		}
		
		
		
	}
	
	public void init(Principal principal) {
		
		String query = "SELECT * FROM planificacion";
		final BaseUtil bd = BaseUtil.getInstance(this);
		ItemAlarm item = new ItemAlarm();
		ArrayList<DatabaseItem> ar = bd.consulta(query, item);
		
		Iterator<DatabaseItem> it = ar.iterator();
		
		while (it.hasNext()) {
			
			item = (ItemAlarm)it.next();
			String[] data = item.getDatosArray();
			AlarmManager am = (AlarmManager)principal.getSystemService(Activity.ALARM_SERVICE);
			
			String[] date = data[2].split("/"); //Date format: dd/mm/yyyy
			int day = Integer.parseInt(date[0]);
			int month = Integer.parseInt(date[1]);
			int year = Integer.parseInt(date[2]);
			
			String[] hourS = data[3].split(":"); //Hour format: hh:mm
			int hour = Integer.parseInt(hourS[0]);
			int min = Integer.parseInt(hourS[1]);
			
			Alarm al = new Alarm(day, month, year, Integer.parseInt(data[4]), hour, min, data[1], Integer.parseInt(data[5]), data[7], data[6],
								am, (Activity)principal);
			al.setId(Integer.parseInt(data[0]));
			
			if (alarmList == null) {
				alarmList = new HashMap<Integer, Alarm>();
			}
			alarmList.put(al.getIdInt(), al);
		}
		
	}

	public static AlarmContainer getInstance() {
		
		if (cont == null) {
			cont = new AlarmContainer();
		}
		
		return cont;
		
	}

	public int getNumAlarm() {
		
		if (alarmList == null)
			return 0;
		return alarmList.size();
		
	}

	public Iterator<Integer> getIterator() {
		
		if (alarmList == null)
			alarmList = new HashMap<Integer, Alarm>();
		
		Set<Integer> set = alarmList.keySet();
		return set.iterator();
	}
	

}
